import { Module } from 'vuex'
import type { App } from './interface'
import { SET_CURRENT_SUB_APP, UPDATE_SUB_APP_LOADING } from './types'

/**
 * 微前端相关
 */
const state: App = {
  currentSubApp: {},
  subAppLloading: false,
}

const app: Module<App, any> = {
  namespaced: true,
  state,
  mutations: {
    [UPDATE_SUB_APP_LOADING](state, isLoading: App['subAppLloading']) {
      state.subAppLloading = isLoading
    },
    [SET_CURRENT_SUB_APP](state, currentSubApp: any) {
      state.currentSubApp = currentSubApp
      const bodyElem = document.getElementsByTagName('body')[0]
      const targetClassList:any = []
      const prefixClass = 'parent--'
      bodyElem.classList.forEach((classStr) => {
        if (!classStr.includes(prefixClass)) {
          targetClassList.push(classStr)
        }
      })
      targetClassList.push(`${prefixClass}${currentSubApp.name}`)
      // @ts-ignore
      bodyElem.classList = targetClassList
    },
  }
}

export default app
